package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.DataBase;
import utils.JdbcUtil;

public class DataBaseDao {
	public List<DataBase> getDBList(){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
		List<DataBase> dbList = null;
		try {
			con = JdbcUtil.getCon();
			String sql = "select * from t_dbs order by dbName";
			dbList = new ArrayList<DataBase>();
			pstmt=con.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				DataBase db = new DataBase();
				db.setId(rs.getInt("id"));
				db.setDbName(rs.getString("dbName"));
				db.setUserName(rs.getString("userName"));
				db.setPassword(rs.getString("password"));
				db.setUrl(rs.getString("url"));
				dbList.add(db);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}
		return dbList;
	}
	public int addDB(DataBase db,String driverName){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
        
		int result=-1;
		try {
			con = JdbcUtil.getCon(); 
			String sql = "insert into t_dbs (dbName,userName,password,url,driverName) values(?,?,?,?,?)";
			pstmt = con.prepareStatement(sql);

			pstmt.setString(1, db.getDbName());
			pstmt.setString(2, db.getUserName());
			pstmt.setString(3, db.getPassword());
			pstmt.setString(4, db.getUrl());
			pstmt.setString(5, driverName);
			result = pstmt.executeUpdate();
			
		}  catch (Exception e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}
		return result;
	}

	public int deleteDB(String id) {
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
        int result = -1;
		try {
			con = JdbcUtil.getCon(); 
			String sql = "delete from dbo.t_dbs where id =" + id;
			pstmt = con.prepareStatement(sql);
			result = pstmt.executeUpdate();
		
		} catch (SQLException e) {
			return 0;
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}
		return result;
	}
	
	public String getDBname(String id){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
		
		String result = null;
		try {
			con = JdbcUtil.getCon(); 
			String sql ="select * from dbo.t_dbs where id="+id;
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next()){
				result = rs.getString("dbName");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}		
	    return result;
		
	}

	public List<DataBase> getDBListbyName(String dbname){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
		
		List<DataBase> dbList = null;
		try {
			con = JdbcUtil.getCon(); 
			String sql = "select * from t_dbs where dbName like '%" +dbname+"%'";
			dbList = new ArrayList<DataBase>();
			pstmt=con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				DataBase db = new DataBase();
				db.setId(rs.getInt("id"));
				db.setDbName(rs.getString("dbName"));
				db.setUserName(rs.getString("userName"));
				db.setPassword(rs.getString("password"));
				db.setUrl(rs.getString("url"));
				db.setDriver(rs.getString("driverName"));
				dbList.add(db);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}	
		return dbList;
	}
	
	public boolean checkDB(DataBase db,String driverName) throws Exception {
		Connection con =null;  
        boolean result = true;
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(db.getUrl(),db.getUserName(),db.getPassword());
			result = con.isClosed();
			con.close();
		}  catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
